package com.simple.business.service.acl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.simple.business.domain.dto.evaluation.EvaluationQuestionDTO;
import com.simple.business.domain.dto.evaluation.EvaluationSaveDTO;
import com.simple.business.domain.entity.evaluation.Evaluation;
import com.simple.business.domain.entity.evaluation.EvaluationQuestion;
import com.simple.business.domain.entity.evaluation.EvaluationReport;
import com.simple.business.domain.enumeration.EvaluationTypeEnum;
import com.simple.business.service.evaluation.EvaluationQuestionService;
import com.simple.business.service.evaluation.EvaluationReportService;
import com.simple.business.service.evaluation.EvaluationService;
import com.simple.business.util.LLMUtil;
import com.simple.business.util.RuleHandleUtil;
import com.simple.llm.config.LLMProperties;
import com.simple.llm.domain.constant.LLMPromptConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc
 * @Author Simple
 * @Date 2025/3/11 10:20
 **/
@Slf4j
@Service
public class EvaluationReportACL {

    @Resource
    private EvaluationQuestionService evaluationQuestionService;
    @Resource
    private EvaluationService evaluationService;
    @Resource
    private EvaluationReportService evaluationReportService;

    @Resource
    private LLMProperties llmProperties;
    @Resource
    private LLMUtil llmUtil;

    /**
     * 保存测评结果
     * @param dto
     */
    public void saveEvaluationReport(EvaluationSaveDTO dto) {
        try {
            Long evaluationId = dto.getId();
            EvaluationTypeEnum evaluationTypeEnum = dto.getEvaluationTypeEnum();
            log.info("开始生成报告: " + evaluationId + ";类型: " + evaluationTypeEnum.getCode());

            //1.保存测评问题
            saveEvaluationQuestions(dto);

            //2.利用大模型生成报告
            EvaluationReport evaluationReport = createEvaluationReport(dto);

            //3.保存测评报告
            evaluationReport.setEvaluationId(evaluationId);
            evaluationReport.setCreateUser(dto.getCreateUser());
            evaluationReport.setCreateTime(LocalDateTime.now());
            evaluationReportService.save(evaluationReport);

            //4.修改测评状态
            //更新状态 0-解析中，1-解析完成，2-异常
            evaluationService.update(Wrappers.lambdaUpdate(Evaluation.class)
                    .set(Evaluation::getParseStatus, 1)
                    .eq(Evaluation::getId, evaluationId));

            log.info("完成报告的生成: " + evaluationId + ";类型: " + dto.getEvaluationTypeEnum().getCode());

        } catch (Exception e) {
            log.error("生成报告: " + e.getMessage());
        }
    }

    /**
     * 保存测评问题
     * @param dto
     */
    public void saveEvaluationQuestions(EvaluationSaveDTO dto) {
        try {
            Long evaluationId = dto.getId();
            List<EvaluationQuestionDTO> evaluationQuestionDTOs = dto.getEvaluationQuestionDTOs();

            if (CollUtil.isNotEmpty(evaluationQuestionDTOs)) {
                List<EvaluationQuestion> evaluationQuestions = new ArrayList<>();
                for (EvaluationQuestionDTO evaluationQuestionDTO : evaluationQuestionDTOs) {
                    EvaluationQuestion evaluationQuestion = new EvaluationQuestion();
                    BeanUtil.copyProperties(evaluationQuestionDTO, evaluationQuestion);
                    evaluationQuestion.setDeleted(Boolean.FALSE);
                    evaluationQuestion.setCreateTime(LocalDateTime.now());
                    evaluationQuestion.setCreateUser(dto.getCreateUser());
                    evaluationQuestion.setEvaluationId(evaluationId);
                    //todo 这里暂时不需要处理语音文件
                    evaluationQuestions.add(evaluationQuestion);
                }
                evaluationQuestionService.saveBatch(evaluationQuestions);
            }

            log.info("保存测评问题成功: " + evaluationId + ";类型: " + dto.getEvaluationTypeEnum().getCode());
        } catch (Exception e) {
            log.error("保存测评问题成功: " + e.getMessage());
        }
    }

    /**
     * 生成报告
     * @param dto
     */
    public EvaluationReport createEvaluationReport(EvaluationSaveDTO dto) {
        EvaluationReport evaluationReport = new EvaluationReport();
        evaluationReport.setDeleted(Boolean.FALSE);

        if (CollUtil.isEmpty(dto.getEvaluationQuestionDTOs())) {
            log.info("生成报告的问题不能为空");
            return evaluationReport;
        }

        for (LLMProperties.GptProperties gptProperties : llmProperties.getGptList()) {
            String res = createDISCAndMBTIAndHollandReport(dto, gptProperties.getModelName(), evaluationReport);
            if (StringUtils.isNotEmpty(res)) {
                return evaluationReport;
            }
        }

        return evaluationReport;
    }


    /**
     * @param dto
     * @param llmName
     */
    private String createDISCAndMBTIAndHollandReport(EvaluationSaveDTO dto, String llmName, EvaluationReport evaluationReport) {
        EvaluationTypeEnum evaluationTypeEnum = dto.getEvaluationTypeEnum();

        List<EvaluationQuestionDTO> evaluationQuestionDTOs = dto.getEvaluationQuestionDTOs();

        if (EvaluationTypeEnum.DISC.eq(evaluationTypeEnum)) {
            // DISC
            String discOrigin = llmUtil.chatWithLlmName("你是一个一个DISC性格测试的专家。你的任务是根据输入的问题和评价标准，结合回答帮助求职者总结DISC。",
                    LLMPromptConstant.AI_INTERVIEW_REPORT_DISC_SISASSISTANT_INIT,
                    evaluationQuestionDTOs,llmName);
            log.info("AI生成disc: " + discOrigin);
            String disc = RuleHandleUtil.fillDISCAndMBTIAndHollandInfo(discOrigin, EvaluationTypeEnum.DISC.getCode());
            evaluationReport.setDisc(disc);
            return disc;
        }

        if (EvaluationTypeEnum.MBTI.eq(evaluationTypeEnum)) {
            // MBTI
            String mbtiOrigin = llmUtil.chatWithLlmName("你是一个MBTI职业测评(性格分析)的专家。你的任务是根据输入的问题和评价标准，结合回答帮助求职者总结MBTI。",
                    LLMPromptConstant.AI_INTERVIEW_REPORT_MBTI_SISASSISTANT_INIT,
                    evaluationQuestionDTOs,llmName);
            log.info("AI生成MBTI: " + mbtiOrigin);
            String mbti = RuleHandleUtil.fillDISCAndMBTIAndHollandInfo(mbtiOrigin, EvaluationTypeEnum.MBTI.getCode());
            evaluationReport.setMbti(mbti);
            return mbti;
        }

        if (EvaluationTypeEnum.HOLLAND.eq(evaluationTypeEnum)) {
            //霍兰德
            String hollandOrigin = llmUtil.chatWithLlmName("你是一个霍兰德职业兴趣理论的专家。你的任务是根据输入的问题和评价标准，结合回答帮助求职者总结霍兰德职业兴趣。",
                    LLMPromptConstant.AI_INTERVIEW_REPORT_HOLLAND_SISASSISTANT_INIT,
                    evaluationQuestionDTOs,llmName);
            log.info("AI生成霍兰德: " + hollandOrigin);
            String holland = RuleHandleUtil.fillDISCAndMBTIAndHollandInfo(hollandOrigin, EvaluationTypeEnum.HOLLAND.getCode());
            evaluationReport.setHolland(holland);
            return holland;
        }
        return "";
    }




}
